package com.jstyle.jclifexd.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.activity.SquadlistActivity;
import com.jstyle.jclifexd.model.Activitvtiesmsg;
import com.jstyle.jclifexd.model.QueryUserStatemsg;
import com.jstyle.jclifexd.model.Teamperformance;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.view.likebutton.LikeButton;
import com.jstyle.jclifexd.view.likebutton.OnLikeListener;
import com.jstyle.jclifexd.view.likebutton.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class TeamperformanceAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private List<Teamperformance> mData;
    private Context context;
    //私有属性
    private OnItemClickListener onItemClickListener = null;
    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //回调接口
    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
    public TeamperformanceAdapter( Activity mycontext ) {
        context=mycontext;
        mData = new ArrayList<>();
    }

    public void setdta(List<Teamperformance> myDatas) {
        this.mData = myDatas;
        notifyDataSetChanged();
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       return   new BaseViewHolder( LayoutInflater.from(context).inflate(R.layout.item_teamperformance,  parent,false));

    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder,final int position) {

       final RelativeLayout teamtileRt = holder.itemView.findViewById(R.id.teamtileRt);//名次等级
        holder.itemView.setTag(position);
        teamtileRt.setTag(position);
        ImageView title_img = holder.itemView.findViewById(R.id.title_img);//名次等级
        TextView zhandui_name = holder.itemView.findViewById(R.id.zhandui_name);//团队名字
        LikeButton likeButton = holder.itemView.findViewById(R.id.likebutton);//点赞
        final LikeButton likebuttoncenten = holder.itemView.findViewById(R.id.likebuttoncenten);
       final TextView likecount = holder.itemView.findViewById(R.id.likecount);//点赞数
        TextView ranking = holder.itemView.findViewById(R.id.Ranking);//名次
        zhandui_name.setText(null==mData.get(position ).getTeamName()?"":mData.get(position ).getTeamName().replace("\\n", "\n"));
        likecount.setText(mData.get(position).getLiked()==0?"0":mData.get(position).getLiked()+"");
        ranking.setText(mData.get(position).getRank()==0?"":mData.get(position).getRank()+"");
        likeButton.setLiked(mData.get(position).isLikedStatus() != 0);

        holder.itemView.findViewById(R.id.centen_Rt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,SquadlistActivity.class);
                intent.putExtra("teamperformance",mData.get(position ));
                context.startActivity(intent);
            }
        });

        likeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked() {
                if(mData.get(position).isLikedStatus()==0){
                    likebuttoncenten.setVisibility(View.VISIBLE);
                    likebuttoncenten.fangfa(2,likebuttoncenten);
                    likecount.setText(mData.get(position).getLiked()+1+"");
                    mData.get(position).setLiked(mData.get(position).getLiked()+1);
                    mData.get(position).setLikedStatus(1);
                    Liked(mData.get(position)) ;
                }
            }
            @Override
            public void unLiked() {
                if(mData.get(position).isLikedStatus()==1){
                likebuttoncenten.fangfa(1,likebuttoncenten);
                likecount.setText(mData.get(position).getLiked()-1+"");
                mData.get(position).setLiked(mData.get(position).getLiked()-1);
                mData.get(position).setLikedStatus(0);
                Liked(mData.get(position)) ;
            } }
        });


        if(0!=mData.get(position).getRank()){
            switch ( mData.get(position).getRank()){
                case 1:
                    title_img.setImageDrawable(context.getResources().getDrawable(R.drawable.good1));
                    break;
                case 2:
                    title_img.setImageDrawable(context.getResources().getDrawable(R.drawable.good2));
                    break;
                case 3:
                    title_img.setImageDrawable(context.getResources().getDrawable(R.drawable.good3));
                    break;
               default:
                    title_img.setImageDrawable(context.getResources().getDrawable(R.drawable.good4));
                    break;
            }
        }else{
            title_img.setImageDrawable(context.getResources().getDrawable(R.drawable.good1));
        }

        //实现点击效果
        teamtileRt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(teamtileRt, position);
                }
            }
        });
    }

    private   void Liked(Teamperformance tt){
        Map<String, Object> map = new HashMap<>();
        map.put("uid", NetWorkUtil.getUserId());
        map.put("teamId", tt.getTeamId()+"");
        map.put("actId", tt.getActId()+"");
        NetWorkUtil.getInstance().getJstyleApi().liked(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesmsg>() {
            @Override
            public void onSubscribe(Disposable d) { }
            @Override
            public void onNext(Activitvtiesmsg activitvtiesData) {

            }
            @Override
            public void onError(Throwable e) { }
            @Override
            public void onComplete() { }
        });
    }
    @Override
    public int getItemCount() {
        //return Integer.MAX_VALUE;
       return mData.size();
    }
}


